package edcom.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

@SuppressWarnings("deprecation")
public class FileUploadBox extends DialogBox {

	private FormPanel formPanel;
	private FileUpload fileUpload;
	private TextBox textBox;
	private TextBox textBox_1;
	private Button btnUpdate;
	private Button btnCancel;
	private VerticalPanel verticalPanel;
	private VerticalPanel verticalPanel_1;
	private HorizontalPanel horizontalPanel;

	public FileUploadBox() {
					
			super(false, false);
			this.setAnimationEnabled(true);
			this.setGlassEnabled(true);
			this.setHTML("Upload a file");

			formPanel = new FormPanel();
			formPanel.setSize("273px", "172px");
			formPanel.setMethod(FormPanel.METHOD_POST);
			formPanel.setEncoding("multipart/form-data");
			formPanel.setAction("upload.jsp");
			formPanel.add(verticalPanel);

			formPanel.addFormHandler(new FormHandler() {

				public void onSubmitComplete(FormSubmitCompleteEvent event) {
					// TODO Auto-generated method stub
					if(event.getResults().trim().isEmpty()){
						Window.alert("File successfully uploaded.");
					} else {
						Window.alert(event.getResults().trim());
					}
					Window.Location.reload();

				}

				@Override
				public void onSubmit(FormSubmitEvent event) {
					// TODO Auto-generated method stub
					if(fileUpload.getFilename().isEmpty()){
						Window.alert("Choose file to upload.");	
						event.setCancelled(true);
					}
				}
			});
			
			
			verticalPanel = new VerticalPanel();
			verticalPanel.setSize("100%", "100%");
			
			verticalPanel_1 = new VerticalPanel();
			formPanel.setWidget(verticalPanel_1);
			verticalPanel_1.setSize("100%", "175px");
			
			fileUpload = new FileUpload();
			fileUpload.setName("file");
			verticalPanel_1.add(fileUpload);
			fileUpload.setSize("270px", "23px");
			
				
			Label lblEnterExerciseNumber = new Label("Enter Exercise Number:");
			verticalPanel_1.add(lblEnterExerciseNumber);
		
			textBox = new TextBox();
			verticalPanel_1.add(textBox);
			textBox.setSize("263px", "15px");
			
			Label lblEnterExerciseTitle = new Label("Enter Exercise Title:");
			verticalPanel_1.add(lblEnterExerciseTitle);
			
			textBox_1 = new TextBox();
			verticalPanel_1.add(textBox_1);
			textBox_1.setSize("263px", "15px");
			
			horizontalPanel = new HorizontalPanel();
			verticalPanel_1.add(horizontalPanel);
			verticalPanel_1.setCellHorizontalAlignment(horizontalPanel, HasHorizontalAlignment.ALIGN_CENTER);
			horizontalPanel.setSize("172px", "21px");
			
			btnUpdate = new Button("Update");
			horizontalPanel.add(btnUpdate);
			btnUpdate.setStyleName("gwt-ButtonEXT");
			btnUpdate.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					// TODO refactor: cookies must be set on the page, not on the widget
					
					Cookies.setCookie("course", "devc140");
					Cookies.setCookie("exernum", textBox.getText());
					Cookies.setCookie("title", textBox_1.getText());
					formPanel.submit();
					
				}
			});
			
			btnCancel = new Button("Cancel");
			horizontalPanel.add(btnCancel);
			horizontalPanel.setCellHorizontalAlignment(btnCancel, HasHorizontalAlignment.ALIGN_RIGHT);
			btnCancel.setStyleName("gwt-ButtonEXT");
			btnCancel.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					hide();
				}
			});
			btnCancel.setWidth("69px");
		
			add(formPanel);
						
	}

}
